const router = require("koa-router")()
const { createOrder, getOrderList } = require("../controller/order")
const { SuccessModel, ErrorModel } = require("../res-module/index")
const loginCheck = require("../middleware/loginCheck")

router.prefix("/api/order")

// 创建订单
router.post("/", loginCheck, async function (ctx, next) {
  const data = ctx.request.body
  //   获取用户信息
  const userInfo = ctx.session.userInfo
  const username = userInfo.username
  try {
    const newOrder = await createOrder(username, data)
    ctx.body = new SuccessModel(newOrder)
  } catch (error) {
    ctx.body = new ErrorModel(10005, "订单创建失败")
  }
})

// 获取订单列表
router.get("/", loginCheck, async function (ctx, next) {
  //   获取用户信息
  const userInfo = ctx.session.userInfo
  const username = userInfo.username
  const list = await getOrderList(username)
  ctx.body = new SuccessModel(list)
})

module.exports = router
